// 添加购物车
const Service = require('egg').Service;

class cartService extends Service {
    async add(query) {
        const { ctx } = this;
        const { id } = query;
        var msg;
        try {
            var data = await ctx.model.Goods.find({_id:id});
            console.log(id);
            /* 2.对cartList进行查询,没有就添加,有就给前端返回对应的值 */
            var res = await ctx.model.User.find({ "cartList._id": id });
            if (res.length == 0) {
                await ctx.model.User.updateOne({ $push: { cartList: data } });
                msg = "添加成功"
            } else {
                msg = "已经添加到购物车"
            }
        } catch (err) {
            msg = "已经添加到购物车"
        }
        return msg
    }
}
module.exports = cartService